package com.bjgzy.courselive.mvp.presenter;

import android.app.Application;

import com.bjgzy.courselive.mvp.contract.CourseIntroductionContract;
import com.bjgzy.courselive.mvp.model.entity.CourseIntroductionData;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * @author xubo
 * @date 2018-8-27 14:58:36
 */
public class CourseIntroductionPresenter extends BasePresenter<CourseIntroductionContract.Model, CourseIntroductionContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public CourseIntroductionPresenter(CourseIntroductionContract.Model model, CourseIntroductionContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void getSummaryById(int internalId) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("internalId", internalId);
        map.put("userId", SPUtils.getInstance().getString(Constants.USERID));
        mModel.getSummaryById(map)
                .retryWhen(new RetryWithDelay(2,3))
                .compose(RxUtil.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<CourseIntroductionData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<CourseIntroductionData> commonData) {
                        if(ObjectUtils.isNotEmpty(commonData.getData())){
                            mRootView.update(commonData.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        mRootView.loadFail();
                    }
                });
    }
}
